<?php
/**
 * @version 1.0
 * @author jonathan <jo.link.noir@gmail.com>
 * @date 19-06-2010
 */
class U1_Planet extends SW_Planet
{
	/** @var array*/
	private $isInitStructure = array();


	/**
	 * Retourne un objet enfant de la planète demander. Si on demande les recherches la position de la planète est automatiquement synchroniser
	 * @param mixed $instance
	 * @param int $idPlanet
	 * @return SW_Planet_Storage_Treatment|null
	 */
	public function getInstance($instance, $idPlanet)
	{
		if ($treatment = parent::getInstance($instance, $idPlanet))
		{
			//sélectionne la planète pour les recherches
			if ($instance === U1_Research::INDEX)
				$treatment->setPositionPlanet($idPlanet);

			//initialise et finit les constructions d'une structure si celle-ci appartient au joueur de la session et qu'elle n'a pas déjà été initialisée
			//ne pas utilisé $treatment->isInit() car finishCompletedLists peut faire appelle à self::getInstance
			if (U1_Player::getSessionLoad() === $this->player)
			{
				$keyInit = $idPlanet.';'.$treatment->__toString();
				if (!isset($this->isInitStructure[$keyInit]))
				{
					$this->isInitStructure[$keyInit] = true;

					//$this->isLoadDependencyStructures[$idPlanet.$instance] = true;
					//fini la construction
					if ($treatment instanceof SW_Structure)
						$treatment->finishCompletedLists($_SERVER['REQUEST_TIME']);

// 					self::initPlanetStorageTreatment($treatment, $instance);
					$treatment->init();
				}
			}
		}

		return $treatment;
	}

	/*
	 * @param SW_Planet_Storage_Treatment $treatment
	 * @param mixed $instance
	 */
	/*static public function initPlanetStorageTreatment(SW_Planet_Storage_Treatment $treatment, $instance)
	{
		if (!($dependency = SW_Dependency::get($instance)))
		{
			$dependency = 'U1_'.ucfirst($instance).'_Dependency';
			$dependency = SW_Dependency::set($instance, new $dependency());
		}
		$treatment->setDependency($dependency);
		$treatment->init();
	}*/

	/**
	 * Initialise l'instance et la retourne si celle-ci n'exista pas.
	 * @param mixed $instance
	 * @param int $idPlanet
	 * @return SW_Planet_Storage_Treatment|null
	 */
	protected function initInstance($instance, $idPlanet)
	{
		if ($instance === U1_Research::INDEX)
		{
			$idPlayer = $this->player->getId();

			if (!($instanceClass = U1_Research::getInstance($this->player)))
			{
				$instanceClass = new U1_Research($this->player);

				//ajoute les recherches
				foreach(U1_Research_DAO::getFactory()->selectAll($idPlayer) as $e)
					$instanceClass->add($e);

				//ajoute les recherches en constructions
				foreach(U1_Research_DAO::getFactory()->selectListAll($idPlayer) as $id_p => $lists)
				{
					$instanceClass->setPositionPlanet($id_p);

					foreach ($lists as $l)
						$instanceClass->addList($l);
				}
			}

			//$instanceClass->setPositionPlanet($idPlanet); //fait par getInstance
			$this->addStorageElement($instanceClass, $instance, $idPlanet);
			return $instanceClass;
		}
		else
		{
			if ($instance === U1_Building::INDEX)
				$instanceClass = 'U1_Building';
			else if ($instance === U1_Army::INDEX)
				$instanceClass = 'U1_Army';
			else if ($instance === U1_Resource::INDEX)
				$instanceClass = 'U1_Resource';
			else
				return null;

			$instanceClass = new $instanceClass($this->player, $idPlanet);
			$this->addStorageElement($instanceClass, $instance, $idPlanet);
			$dao = $instanceClass->getDao();

			//Remplit les structure des données de la bdd
			foreach($dao->selectAllInPlanet($idPlanet) as $e)
				$instanceClass->add($e);

			if ($instance !== U1_Resource::INDEX)
			{
				//ajoute les listes de constructions
				foreach($dao->selectListAllInPlanet($idPlanet) as $l)
					$instanceClass->addList($l);
			}

			return $instanceClass;
		}
	}

	/**
	 * Récupère l'objet U1_Planet qui contient l'élément $planet
	 * @param SW_Planet_Element $planet
	 * @return U1_Planet|null
	 */
	static public function factoryContainsPlanetElement(SW_Planet_Element $planet)
	{
		return ($player = U1_DAO_User::select($planet->getRefUser()))
			? $player->offsetGet(self::INDEX)
			: null;
	}

	/**
	 * Récupère l'objet U1_Planet qui contient la planète d'id $id
	 * @param int $id_planet
	 * @return U1_Planet|null
	 */
	static public function factoryContainsPlanetId($id_planet)
	{
		return ($planet = U1_DAO_Planet::selectId($id_planet))
			? self::factoryContainsPlanetElement($planet)
			: null;
	}
}